/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import Game.bc.WordFactory;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;

/**
 *
 * @author Light
 */
@Stateless
public class HelloService implements HelloServiceLocal {

    private ArrayList<String> words3 = new ArrayList<String>();
    private ArrayList<String> words4 = new ArrayList<String>();
    private ArrayList<String> words5 = new ArrayList<String>();
    private ArrayList<String> words6 = new ArrayList<String>();
    private ArrayList<String> words7 = new ArrayList<String>();
    private Random randomGenerator = new Random();
    private Boolean isReady = false;

    @PostConstruct
    public void loadWords() {
        try {
            words3 = getWordFiles("http://users.groept.be/as/ashish.vaidya/site/words/3.txt");
            words4 = getWordFiles("http://users.groept.be/as/ashish.vaidya/site/words/4.txt");
            words5 = getWordFiles("http://users.groept.be/as/ashish.vaidya/site/words/5.txt");
            words6 = getWordFiles("http://users.groept.be/as/ashish.vaidya/site/words/6.txt");
            words7 = getWordFiles("http://users.groept.be/as/ashish.vaidya/site/words/7.txt");
            isReady = true;
        } catch (Exception ex) {
            isReady = false;
            Logger.getLogger(WordFactory.class.getName()).log(Level.SEVERE, null, ex);

        }
    }

    /*Makes Hppt request and gets the file contents.*/
    private ArrayList<String> getWordFiles(String wordFileURLString) throws Exception {
        URL wordFileURL = new URL(wordFileURLString);
        ArrayList<String> tempWordArray = new ArrayList<String>();
        URLConnection con = wordFileURL.openConnection();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                con.getInputStream()));
        String inputLine;
        while (((inputLine = in.readLine()) != null)) {
            tempWordArray.add(inputLine);
        }
        return tempWordArray;
    }

    public String getWord(int wordLength) {
        if (isReady) {
            switch (wordLength) {
                case 3:
                    return words3.get(randomGenerator.nextInt(words3.size()));
                case 4:
                    return words4.get(randomGenerator.nextInt(words4.size()));
                case 5:
                    return words5.get(randomGenerator.nextInt(words5.size()));
                case 6:
                    return words6.get(randomGenerator.nextInt(words6.size()));
                case 7:
                    return words7.get(randomGenerator.nextInt(words7.size()));
                default:
                    return "INVALID WORD LENGTH PLEASE INPUT A NUMBER BETWEEN 3 TO 7";
            }


        } else {
            try {
                loadWords();
            } catch (Exception ex) {
                Logger.getLogger(WordFactory.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


        return "ERROR !!!";
    }

    public String sayHello(String name) {
      //  loadWords();

        try {
            return "The Word is :" + name + " " + isReady + getWord(5) + "!";
        } catch (Exception ex) {
            Logger.getLogger(HelloService.class.getName()).log(Level.SEVERE, null, ex);
            return name;
        }
    }
}
